# ========= 导入基础模块 ============
from resources_base.module_base.importbase import *
# ========= 导入数据的类 ============
from trainweb.models import Trainee, Record, RecordDetail


def index(request):
    """展示阶段详情页面"""
    return render(request, 'train/phase_detail.html')


def get_phase_basic_info(request):
    """获取当前阶段的基础信息"""
    # 获取传递的信息
    eid = request.POST.get('eid')
    position_id = request.POST.get('position_id')
    phase_name = request.POST.get('phase_name')
    # 获取数据
    objs = list(Record.objects.filter(trainee__eid=eid, trainee__position_id=position_id,
                                      phase__phasename__name=phase_name).values())
    # 判断是否存在
    if objs:
        return JsonResponse({'status': True, 'data': objs[0]})
    else:
        return JsonResponse({'status': False})


def get_echarts_data(request):
    """获取echarts中所需要的总培训时长和总工时"""
    # 获取传递的信息
    eid = request.POST.get('eid')
    position_id = request.POST.get('position_id')
    phase_name = request.POST.get('phase_name')
    # 获取record记录
    obj_record = list(Record.objects.filter(trainee__eid=eid, trainee__position_id=position_id,
                                            phase__phasename__name=phase_name).values())
    # 定义两组数据
    training_time_list = []  # 当前用户的，最大的，最小的，平均的
    working_time_list = []  # 当前用户的，最大的，最小的，平均的
    # 判断obj_record是否有记录
    if obj_record:
        training_time_list.append(obj_record[0].get('total_training'))
        working_time_list.append(obj_record[0].get('total_working'))
    else:
        training_time_list.append(0)
        working_time_list.append(0)

    # 获取所有完成当前阶段的用户
    obj_record = list(
        Record.objects.filter(trainee__position_id=position_id, phase__phasename__name=phase_name).values())
    # 定义三个变量
    training_dict = {'max': 0, 'min': 10000, 'mean': 0, 'sum': 0}
    working_dict = {'max': 0, 'min': 10000, 'mean': 0, 'sum': 0}
    # 遍历
    for value in obj_record:
        # 培训时长
        # 判断
        if training_dict['max'] < value.get('total_training'):
            training_dict['max'] = value.get('total_training')
        if training_dict['min'] > value.get('total_training'):
            training_dict['min'] = value.get('total_training')
        training_dict['sum'] += value.get('total_training')

        # 工时
        if working_dict['max'] < value.get('total_working'):
            working_dict['max'] = value.get('total_working')
        if working_dict['min'] > value.get('total_working'):
            working_dict['min'] = value.get('total_working')
        working_dict['sum'] += value.get('total_working')

    # 附加培训时长的最大，最小和平均
    training_time_list.append(training_dict['max'])
    training_time_list.append(training_dict['min'])
    training_time_list.append(training_dict['sum'] / len(obj_record))

    # 附加总工时的最大，最小和平均
    working_time_list.append(working_dict['max'])
    working_time_list.append(working_dict['min'])
    working_time_list.append(working_dict['sum'] / len(obj_record))

    return JsonResponse({'training': training_time_list, 'working': working_time_list})


def get_phase_detail(request):
    """获取当前用户在当前职位当前阶段的完成详情"""
    # 获取传递的信息
    eid = request.POST.get('eid')
    position_id = request.POST.get('position_id')
    phase_name = request.POST.get('phase_name')
    # 获取内容详情
    obj_details = list(RecordDetail.objects.filter(record__trainee__position_id=position_id, record__trainee__eid=eid,
                                                   record__phase__phasename__name=phase_name).values('id','training_time', 'working_time','record_time',
                                                                                                     'remark','status__name', 'phasecontent__name'))
    # 返回
    return JsonResponse({'data': obj_details})
